﻿<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../Scripts/jquery-3.3.1.js"></script>
    <script src="../layui/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style>
        .layui-inline .layui-input {
            height: 30px;
        }
        .layui-form-item .layui-btn+.layui-btn {
            margin-left: 60px;
        }
    </style>
</head>

<body style="margin:20px 20px;">
    <div class="search">
        <div class="layui-inline">
            <input class="layui-input" name="keyword" id="student_name" placeholder="姓名" autocomplete="off">
        </div>
        <div class="layui-inline">
            <input class="layui-input" name="keyword" id="student_id" placeholder="学号" autocomplete="off">
        </div>
        <button class="layui-btn layui-btn-normal  layui-btn-sm" id="search_btn" data-type="reload">搜索</button>
    </div>
    
  <div class="layui-btn-container" id="toolbarDemo" style="display:none">
    <button class="layui-btn layui-btn-sm" id="add_btn" lay-event="AddData" >添加</button>
  </div>
    <!-- 表格行操作工具按钮 -->
<div style="display:none" id="barDemo">
        
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </div>
    <div>
        <table class="layui-hide" id="student_set" lay-filter="student_set"></table>
    </div>
    <div class="layui-tab-item">
        <div id="pageDemo"></div>
    </div>

    <!-- 编辑的弹出层 -->
    <div class="UpdateData" id="UpdateData" style="display: none;padding: 20px">
        <form class="layui-form" action="" style="margin: 5px 20px;" id="UpdateDataFrom">
            <div class="layui-form-item">
                <label class="layui-form-label">学号：</label>
                <div class="layui-input-block">
                    <input type="text" name ="student_id" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名：</label>
                <div class="layui-input-block">
                    <input type="text" name="student_name" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">学院：</label>
                <div class="layui-input-block">
                    <input type="text" name="college_name" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">专业：</label>
                <div class="layui-input-block">
                    <input type="text" name="major_name" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别：</label>
                <div class="layui-input-block">
                    <input type="text" name ="student_sex" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系电话：</label>
                <div class="layui-input-block">
                    <input type="text" name="student_tel" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">身份证号：</label>
                <div class="layui-input-block">
                    <input type="text" name="student_id_card" class="layui-input">
                </div>
            </div>
        </form>
    </div>
   
    <!-- 添加的弹出层 -->
     <div class="AddData" id="AddData" style="display:none; padding: 20px">
        <form class="layui-form" lay-filter="" id="AddDataForm" style="margin-left:40px;">
            
            <div class="layui-form-item">
                <label class="layui-form-label">学号</label>
                <div class="layui-input-inline">
                    <input type="text" name="student_id" placeholder="请输入请输入学号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="student_pwd" placeholder="请输入密码" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="student_name" placeholder="请输入姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-inline">
                    <input type="tel" autocomplete="off" name="student_tel" placeholder="请输入联系电话" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item form-inline">
                <label class="layui-form-label">专业</label>
                <div class="layui-input-inline">
                    <select name="major_name" class="majorSelect" lay-search lay-verify="">
                        
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单选框</label>
                <div class="layui-input-block">
                    <input type="radio" name="student_sex" value="男" title="男" checked="">
                    <input type="radio" name="student_sex" value="女" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">身份证号</label>
                <div class="layui-input-inline">
                    <input type="text" name="student_id_card" autocomplete="off" placeholder="请输入身份证号" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">头像</label>
                <div class="layui-input-inline">
                    <input name="student_photo_path" id="photoUploadPath" placeholder="图片地址" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-inline layui-btn-container" style="width:auto;">
                    <button type="button" class="layui-btn layui-btn-primary" id="photoUpload"
                        style="width:190px;margin-left: 110px;">
                        <i class="layui-icon"></i>上传图片
                    </button><input class="layui-upload-file" accept type="file"   name="file">
                </div>
            </div>
            <!--<div class="layui-form-item" style="margin-left: 50px;">
                <button class="layui-btn"   id="confire_btn">确认修改</button>
                <button type="reset" class="layui-btn layui-btn-primarylayui-input-inline" id="reset_btn">重新填写</button>
                
            </div>-->
        </form>
    </div>
    
    <script>
        
    </script>

    <script>
        layui.use(['table', 'laypage', 'layer', 'form','upload'], function ()
        {
            var table = layui.table,
                laypage = layui.laypage,
                form = layui.form,
                upload = layui.upload;

            //专业
            var majorSet;
            $.ajax({
                url: '../operate/getSet.aspx',
                datatype: 'text',
                data: { "table": "tb_major" },
                success: function (res)
                {
                    //把数据填充到专业下拉框
                    majorSet = res;
                    var major = majorSet.split(',');
                    var html1 = '<dd lay-value="" class="layui-select-tips layui-this">直接选择或搜索选择</dd>';
                    var html2 = '<option value="" >直接选择或搜索选择</option>';
                    for (var i = 0; i < major.length; i++)
                    {
                        html1 += '<dd lay-value="' + i + '">' + major[i] + '</dd>';
                        html2 += '<option value="' + i + '">' + major[i] + '</option>';
                    }
                    var select = $(".majorSelect");
                        $(".majorSelect").next().children().eq(1).html(html1);//dd  是渲染代码
                        $(".majorSelect").html(html2); //option 选项代码
                        form.render();//没有写这个，操作后没有效果
                }
            });
            var ins1 = table.render({ //相当于ajax
                elem: '#student_set'
                , url: 'studentManage.aspx'
                , id: 'student_set1' //重载时使用
                , title: '学生基本信息表'
                , height: 520
                , page: true
                , limits: [3, 5, 10]  //一页选择显示3,5或10条数据
                , limit: 10  //一页显示10条数据
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
               , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'print', { 
                    title: '导出'
                  , layEvent: 'export'
                  , icon: 'layui-icon-export'
                }]
                , cols: [[
                    { field: 'student_id', width: 200, title: '学号', sort: true }
                    , { field: 'student_name', width: 100, title: '姓名' }
                     , { field: 'college_name', width: 120, title: '学院' }
                      , { field: 'major_name', width: 180, title: '专业' }
                       , { field: 'student_sex', width: 60, title: '性别' }
                       , { field: 'student_tel', width: 140, title: '联系电话' }
                       , { field: 'student_id_card', width: 240, title: '身份证号' }
                    , { fixed: 'right', width: 165, align: 'center',title:'操作', toolbar: '#barDemo' }//添加列工具条
                ]]
                , parseData: function (res)
                { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                    console.log(JSON.stringify(res));
                    console.log(JSON.stringify(res.code));
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data //解析数据列表
                    };
                }
                , error: function (xhr, state, errorThrown)
                {
                    requesFail(xhr);
                }
            })
            //监听行工具事件
            table.on('tool(student_set)', function (obj)
            { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值

                //console.log(data);

                if (layEvent === 'del')
                {
                    layer.confirm('真的删除行么', function (index)
                    {
                        layer.close(index);
                        console.log(data);
                        console.log(JSON.stringify(obj));
                        //向服务端发送删除指令
                        delStudentInfo(data);
                    });
                } else if (layEvent === 'edit')
                {
                    //layer.msg('编辑操作');
                    //页面层
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['420px', '600px'], //宽高
                        offset: '10px',
                        content: $("#UpdateData"),
                        btn: ['保存更改']
                        , success: function (data)
                        {
                            getData();//填充数据
                            $("#UpdateDataFrom input[name=student_id]").attr("ReadOnly", true);//不可更改
                        }
                        , yes: function (index, layero)
                        {
                            updateUserInfo();
                            //return false;
                        }
                    });
                }
                //对表单数据赋值
                function getData()
                {
                    var from = $("#UpdateDataFrom .layui-input");
                    for (var i = 0; i < from.length; i++)
                    {
                        var key = from[i].name;
                        var value = data[key];
                        from[i].value =value;
                    }


                }
               function  delStudentInfo(data){
                    $.ajax({
                        url: '../operate/operateUserInfo.aspx',
                        datatype: 'text',
                        data: { 'operate': layEvent, 'table': 'tb_student', 'student_id': data.student_id },
                        success: function (res)
                        {
                            if (res > 0)
                            {
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.msg('删除成功');
                                $(".layui-laypage-btn").click();//刷新当前分页数据
                            }
                            else
                            {
                                layer.msg('删除失败，不存在该用户');
                            }
                        },
                        error: function (xhr, textStatus, errorThrown)
                        {
                            alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                            //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                        }
                    });
                }
            });
            function updateStudentInfo(para)
            {
                $.ajax({
                    url: '../operate/operateUserInfo.aspx',
                    datatype: 'text',
                    data: para,
                    success: function (res)
                    {
                        if (res > 0)
                        {
                            layer.msg('更改成功');
                            $(".layui-laypage-btn").click();//刷新当前分页数据
                        }
                        else 
                        {
                            layer.msg('更改失败，数据不对');
                        }
                    },
                    error: function (xhr, textStatus, errorThrown)
                    {
                        alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                        //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                    }
                });
            }
            //头工具栏事件
            table.on('toolbar(student_set)', function (obj)
            {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event)
                {
                    case 'AddData':
                        //页面层
                        layer.open({
                            type: 1,
                            title:'添加用户',
                            skin: 'layui-layer-rim', //加上边框
                            area: ['450px', '650px'], //宽高
                            content: $("#AddData"),
                            btn: ['保存']
                            , yes: function (index, layero)
                            {
                                addUserInfo();
                                //return false;
                            }
   

                        });
                        break;
                    

                        //自定义头工具栏右侧图标 - 导出
                    case 'export':
                        layer.confirm('是否导出数据！', {
                            btn: ['确定', '取消'] //可以无限个按钮
                            ,offset:'10px'
                            , btn2: function (index, layero)
                            {

                                return true;//按钮【按钮二】的回调
                            }
                        }, function (index)
                        {

                            layer.close(index);//按钮【按钮一】的回调
                            exportFile();
                        });

                        break;
                };
                
            });
            function exportFile()
            {
                var student_id = $('#student_id').val();
                var student_name = $('#student_name').val();
                var para = {};
                para.student_id = student_id;
                para.student_name = student_name;
                $.ajax({
                    url: '../export/studentExport.aspx',
                    methods: 'post',
                    dataType: 'json',
                    data: para,
                    success: function (res)
                    {
                        table.exportFile(ins1.config.id, res.data);
                    },
                    error: function (res)
                    {
                        alert("导出失败");
                    }
                });

            }
            $('#search_btn').click(function ()
            {
                var student_id = $('#student_id').val();
                var student_name = $('#student_name').val();
                var para = {};
                para.student_id = student_id;
                para.student_name = student_name;
                console.log(para);
                table.reload('student_set1', {
                    url: 'studentManage.aspx'
                    , methods: "post"
                    ,where:para
                    , page: {
                        curr: 1
                    }
                });
            });
            $('#save_btn').click(function ()
            {
                //获取表单所有值
                var from = $("#UpdateDataFrom .layui-input");
                var para = {};
                para["operate"] = "update";
                para["table"] = "tb_student";
                for (var i = 0; i < from.length; i++)
                {
                    var key = from[i].name;
                    para[key] = from[i].value;
                }
                console.log("保存：" + para);
                updateStudentInfo(para);
            });
            function updateUserInfo()
            {

                //获取表单所有值
                var from = $("#UpdateDataFrom .layui-input");
                var para = {};
                para["operate"] = "update";
                para["table"] = "tb_student";
                for (var i = 0; i < from.length; i++)
                {
                    var key = from[i].name;
                    para[key] = from[i].value;
                }
                
                $.ajax({
                    url: '../operate/operateUserInfo.aspx',
                    datatype: 'text',
                    data: para,
                    success: function (res)
                    {
                        if (res > 0)
                        {
                            layer.msg('添加成功');
                        }
                        else if (res = 0)
                        {
                            layer.msg('添加失败，该学号已经存在');
                        }
                        else
                        {
                            layer.msg('数据类型不对!');
                        }
                    },
                    error: function (xhr, textStatus, errorThrown)
                    {
                        alert("状态码：" + xhr.status + "状态:" + xhr.readyState + "错误信息:" + xhr.statusText + "请求状态：" + textStatus + errorThrown);
                        //当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
                    }
                });
            }
           function addUserInfo()
            {
                //保存按钮
                //获取所有输入值
                var items = $('#AddDataForm')[0];
                var length = items.length;
                var para = {};
                para["operate"] = "add";
                para["table"] = "tb_student";
                for (var i = 0; i < length; i++)
                {
                    var item = items[i];
                    var t = item.type;
                    switch (item.type)
                    {
                        case 'text':
                            para[item.name] = item.value;
                            break;
                        case 'tel':
                            para[item.name] = item.value;
                            break;
                        case 'radio':
                            if (item.checked == true)
                            {
                                para[item.name] = item.value;
                            }
                            break;
                        case 'select-one':
                            if (item.value == "") alert("请选择专业");
                            para[item.name] = item[item.selectedIndex].text;
                            break;
                        case 'password':
                            para[item.name] = item.value;
                            break;
                        default:   
                    }
                }
               //上传到后台
                $.ajax({
                    url: '../operate/operateUserInfo.aspx',
                    datatype: 'text',
                    methed:'post',
                    data: para,
                    success: function (res)
                    {
                        if (res > 0)
                            alert("添加成功");
                        else if (res < 0)
                        {
                            alert("存在该用户");
                        }else
                            alert("添加失败");
                    }
                });
                
                //alert('parass' + JSON.stringify(para));

            }
           
               
           

        });

        //上传文件
        layui.use('upload', function ()
        {
            var upload = layui.upload;
            var formData = new FormData();
            //执行实例
            var uploadInst = upload.render({
                elem: '#photoUpload' //绑定元素
                 , url: '../fileAspx/transFile.aspx', //上传接口
                cache: false,//不需要缓存
                processData: false,
                contentType: false,
                accept: 'images',
                done: function (res)
                {
                    //上传完毕回调
                    
                    $("#photoUploadPath").val(res.data.src);//保存图片路径名称，然后存入数据库
                    alert("图片上传成功");
                }
              , error: function ()
              {
                  //请求异常回调
                  alert("图片上传失败");
              }
            });
        });

    </script>
    
</body>

</html>